package com.scuec.trainingmanagementsys.controller;

import com.scuec.trainingmanagementsys.entity.WeeklyAssessment;
import com.scuec.trainingmanagementsys.service.WeeklyAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/weekly-assessments")
@CrossOrigin
public class WeeklyAssessmentController {
    @Autowired
    private WeeklyAssessmentService weeklyAssessmentService;

    // 增加周评估记录
    @PostMapping("/insert")
    public ResponseEntity<WeeklyAssessment> addWeeklyAssessment(@RequestBody WeeklyAssessment weeklyAssessment) {
        // 自动设置创建时间
        if (weeklyAssessment.getWsCreateTime() == null) {
            weeklyAssessment.setWsCreateTime(new Date());
        }

        int result = weeklyAssessmentService.addWeeklyAssessment(weeklyAssessment);
        return result > 0 ? ResponseEntity.ok(weeklyAssessment) : ResponseEntity.badRequest().build();
    }

    // 删除周评估记录
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteWeeklyAssessment(@PathVariable("id") Integer weeklyAssessmentID) {
        int result = weeklyAssessmentService.deleteWeeklyAssessmentById(weeklyAssessmentID);
        return result > 0 ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }

    // 根据ID查找周评估记录
    @GetMapping("/{id}")
    public ResponseEntity<WeeklyAssessment> findWeeklyAssessmentById(@PathVariable("id") Integer weeklyAssessmentID) {
        WeeklyAssessment weeklyAssessment = weeklyAssessmentService.findWeeklyAssessmentById(weeklyAssessmentID);
        return weeklyAssessment != null ? ResponseEntity.ok(weeklyAssessment) : ResponseEntity.notFound().build();
    }

    // 查找所有周评估记录
    @GetMapping("/list")
    public ResponseEntity<List<WeeklyAssessment>> findAllWeeklyAssessments() {
        List<WeeklyAssessment> assessments = weeklyAssessmentService.findAllWeeklyAssessments();
        return ResponseEntity.ok(assessments);
    }

    // 根据教师ID查找周评估记录
    @GetMapping("/teacher/{teacherId}")
    public ResponseEntity<List<WeeklyAssessment>> findWeeklyAssessmentsByTeacherId(@PathVariable("teacherId") Integer teacherID) {
        List<WeeklyAssessment> assessments = weeklyAssessmentService.findWeeklyAssessmentsByTeacherId(teacherID);
        return ResponseEntity.ok(assessments);
    }

    // 修改周评估记录
    @PutMapping("/{id}")
    public ResponseEntity<WeeklyAssessment> updateWeeklyAssessment(@PathVariable("id") Integer weeklyAssessmentID,
                                                                   @RequestBody WeeklyAssessment weeklyAssessment) {
        if (!weeklyAssessmentID.equals(weeklyAssessment.getWeeklyAssessmentID())) {
            return ResponseEntity.badRequest().build();
        }
        int result = weeklyAssessmentService.updateWeeklyAssessment(weeklyAssessment);
        return result > 0 ? ResponseEntity.ok(weeklyAssessment) : ResponseEntity.notFound().build();
    }

    // 根据周报ID查找周评估记录
    @GetMapping("/weekly-report/{weeklyReportID}")
    public ResponseEntity<List<WeeklyAssessment>> findWeeklyAssessmentsByWeeklyReportID(@PathVariable("weeklyReportID") Integer weeklyReportID) {
        List<WeeklyAssessment> assessments = weeklyAssessmentService.findWeeklyAssessmentsByWeeklyReportID(weeklyReportID);
        return ResponseEntity.ok(assessments);
    }

    // 根据周报ID查找周评估记录（含教师信息）
    @GetMapping("/weekly-report/details/{weeklyReportID}")
    public ResponseEntity<List<Map<String, Object>>> findWeeklyAssessmentsByWeeklyReportIDWithDetails(@PathVariable("weeklyReportID") Integer weeklyReportID) {
        List<Map<String, Object>> assessments = weeklyAssessmentService.findWeeklyAssessmentsByWeeklyReportIDWithDetails(weeklyReportID);
        return ResponseEntity.ok(assessments);
    }
    @GetMapping("/getCommentsByReportId/{reportId}")
    public ResponseEntity<Map<String, Object>> getCommentsByReportId(
            @PathVariable Integer reportId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Map<String, Object>> comments = weeklyAssessmentService.getCommentsByReportId(reportId);
            result.put("code", 200);
            result.put("message", "获取评语成功");
            result.put("data", comments);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取评语失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }
}